package com.woniu34.user.controller;


import com.woniu.common.constant.Constants;
import com.woniu.common.core.domain.AjaxResult;
import com.woniu.common.utils.sign.Md5Utils;
import com.woniu34.user.entity.User;
import com.woniu34.user.service.UserService;
import com.woniu34.user.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Set;

@RestController
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("verifyCode")
    public String queryVerifyCode(String cellphone){
        return userService.queryVerifyCode( cellphone);
    }

    @RequestMapping("findByUserAccountAndPassword")
    public User findByUserAccountAndPassword(String userAccount, String password){
        return userService.findByUserAccountAndPassword(userAccount,password);
    }


    @RequestMapping("register")
    public AjaxResult register(@RequestBody  UserVo userVo){

        Boolean flag =  userService.register( userVo.getCellphone(), userVo.getVerifyCode(), userVo.getUserPassword());

        return AjaxResult.success();

    }

    @RequestMapping("getInfo")
    public AjaxResult getInfo(HttpServletRequest request){
        AjaxResult ajax = AjaxResult.success();

        String userIdStr = request.getHeader("userid");
        if (userIdStr != null) {
            Integer userId = Integer.valueOf(userIdStr);
            //由于目前只有一个admin用户，这里就不查询数据库了，直接返回超级用户的权限
            Set<String> roles =  new HashSet<>();
            roles.add("admin");
            Set<String> perms = new HashSet<String>();
            perms.add("*:*:*");
            User user = new User();
            user.setUserName("admin");
            user.setUserAccount("admin");
            user.setUserId(1L);
            ajax.put("user",user );
            ajax.put("roles", roles);
            ajax.put("permissions", perms);
        }
        return ajax;
    }

    public static void main(String[] args) {
        System.out.println(Md5Utils.hash("111" + Constants.SALT));
    }
}
